package org.dromara.exam.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.exam.domain.bo.ExamHistoryPaperBo;
import org.dromara.exam.domain.vo.ExamHistoryPaperVo;
import org.dromara.exam.domain.ExamHistoryPaper;
import org.dromara.exam.mapper.ExamHistoryPaperMapper;
import org.dromara.exam.service.IExamHistoryPaperService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 历年题库预览Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExamHistoryPaperServiceImpl implements IExamHistoryPaperService {

    private final ExamHistoryPaperMapper baseMapper;

    /**
     * 查询历年题库预览
     *
     * @param paperId 主键
     * @return 历年题库预览
     */
    @Override
    public ExamHistoryPaperVo queryById(Long paperId){
        return baseMapper.selectVoById(paperId);
    }

    /**
     * 分页查询历年题库预览列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 历年题库预览分页列表
     */
    @Override
    public TableDataInfo<ExamHistoryPaperVo> queryPageList(ExamHistoryPaperBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamHistoryPaper> lqw = buildQueryWrapper(bo);
        Page<ExamHistoryPaperVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的历年题库预览列表
     *
     * @param bo 查询条件
     * @return 历年题库预览列表
     */
    @Override
    public List<ExamHistoryPaperVo> queryList(ExamHistoryPaperBo bo) {
        LambdaQueryWrapper<ExamHistoryPaper> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamHistoryPaper> buildQueryWrapper(ExamHistoryPaperBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamHistoryPaper> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ExamHistoryPaper::getPaperId);
        lqw.eq(StringUtils.isNotBlank(bo.getPaperYearMonth()), ExamHistoryPaper::getPaperYearMonth, bo.getPaperYearMonth());
        lqw.eq(bo.getPaperLevel() != null, ExamHistoryPaper::getPaperLevel, bo.getPaperLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), ExamHistoryPaper::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getPdfDownloadUrl()), ExamHistoryPaper::getPdfDownloadUrl, bo.getPdfDownloadUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPdfPreviewUrl()), ExamHistoryPaper::getPdfPreviewUrl, bo.getPdfPreviewUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), ExamHistoryPaper::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增历年题库预览
     *
     * @param bo 历年题库预览
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ExamHistoryPaperBo bo) {
        ExamHistoryPaper add = MapstructUtils.convert(bo, ExamHistoryPaper.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPaperId(add.getPaperId());
        }
        return flag;
    }

    /**
     * 修改历年题库预览
     *
     * @param bo 历年题库预览
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ExamHistoryPaperBo bo) {
        ExamHistoryPaper update = MapstructUtils.convert(bo, ExamHistoryPaper.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamHistoryPaper entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除历年题库预览信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
